<script setup lang="ts">
import { onMounted, ref } from 'vue'

// 图书列表数据
const books = ref([
  {
    id: 1,
    title: '三体',
    author: '刘慈欣',
    cover: '/static/images/books/santi.jpg',
    description: '文化大革命如火如荼进行的同时，军方探寻外星文明的绝秘计划"红岸工程"取得了突破性进展...',
    favoriteTime: '2025-03-15 10:00:00',
  },
  {
    id: 2,
    title: '活着',
    author: '余华',
    cover: '/static/images/books/huozhe.jpg',
    description: '《活着》是余华的代表作，讲述了一个人一生的故事，表达了"活着"的深刻含义...',
    favoriteTime: '2025-03-14 15:30:00',
  },
])

// 加载状态
const loading = ref(false)
const hasMore = ref(true)

// 格式化时间
function formatTime(time: string) {
  const date = new Date(time)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 跳转到图书详情
function goToDetail(id: number) {
  uni.navigateTo({
    url: `/pages/book/detail/index?id=${id}`,
  })
}

// 跳转到浏览页面
function goToBrowse() {
  uni.switchTab({
    url: '/pages/index/index',
  })
}

// 取消收藏
function cancelFavorite(id: number) {
  uni.showModal({
    title: '提示',
    content: '确定要取消收藏吗？',
    success: (res) => {
      if (res.confirm) {
        // 模拟取消收藏
        const index = books.value.findIndex(book => book.id === id)
        if (index > -1) {
          books.value.splice(index, 1)
          uni.showToast({
            title: '已取消收藏',
            icon: 'success',
          })
        }
      }
    },
  })
}

// 加载更多数据
function loadMore() {
  if (loading.value || !hasMore.value) {
    return
  }

  loading.value = true

  // 模拟加载更多数据
  setTimeout(() => {
    loading.value = false
    hasMore.value = false
  }, 1000)
}

// 页面加载
onMounted(() => {
  // 模拟初始数据加载
  loading.value = true
  setTimeout(() => {
    loading.value = false
  }, 1000)
})
</script>

<template>
  <view class="container">
    <!-- 图书列表 -->
    <view v-if="books.length > 0" class="book-list">
      <view v-for="book in books" :key="book.id" class="book-item" @click="goToDetail(book.id)">
        <image class="cover" :src="book.cover" mode="aspectFill" />
        <view class="info">
          <view class="title">
            {{ book.title }}
          </view>
          <view class="author">
            {{ book.author }}
          </view>
          <view class="desc">
            {{ book.description }}
          </view>
          <view class="time">
            收藏时间：{{ formatTime(book.favoriteTime) }}
          </view>
        </view>
        <view class="delete-btn" @click.stop="cancelFavorite(book.id)">
          <text class="iconfont icon-delete" />
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-else class="empty">
      <image class="empty-icon" src="/static/images/empty.svg" mode="aspectFit" />
      <view class="empty-text">
        暂无收藏的图书
      </view>
      <view class="browse-btn" @click="goToBrowse">
        去浏览
      </view>
    </view>

    <!-- 加载更多 -->
    <view v-if="books.length > 0" class="load-more">
      <text v-if="loading">
        加载中...
      </text>
      <text v-else-if="!hasMore">
        没有更多了
      </text>
    </view>
  </view>
</template>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 30rpx;
}

.book-list {
  .book-item {
    position: relative;
    display: flex;
    background: #fff;
    border-radius: 12rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;

    .cover {
      width: 160rpx;
      height: 220rpx;
      border-radius: 8rpx;
      margin-right: 20rpx;
    }

    .info {
      flex: 1;
      display: flex;
      flex-direction: column;

      .title {
        font-size: 32rpx;
        color: #333;
        font-weight: bold;
        margin-bottom: 10rpx;
      }

      .author {
        font-size: 28rpx;
        color: #666;
        margin-bottom: 10rpx;
      }

      .desc {
        font-size: 26rpx;
        color: #999;
        line-height: 1.6;
        margin-bottom: 10rpx;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }

      .time {
        font-size: 24rpx;
        color: #999;
      }
    }

    .delete-btn {
      position: absolute;
      right: 20rpx;
      top: 20rpx;
      width: 40rpx;
      height: 40rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      .iconfont {
        font-size: 36rpx;
        color: #999;
      }
    }
  }
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 100rpx 0;

  .empty-icon {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 30rpx;
  }

  .empty-text {
    font-size: 28rpx;
    color: #999;
    margin-bottom: 40rpx;
  }

  .browse-btn {
    width: 240rpx;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    background: #4caf50;
    border-radius: 40rpx;
    font-size: 28rpx;
    color: #fff;
  }
}

.load-more {
  text-align: center;
  padding: 30rpx 0;

  text {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
